package com.schd.base.service;

import com.google.common.collect.Lists;
import com.schd.base.enumerate.SearchOperator;
import com.schd.base.model.Search;
import com.schd.base.model.SearchCondition;
import com.schd.base.repository.BaseRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.io.Serializable;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 创建人: 陈刚
 * 日期: 2017/8/10
 * 时间: 18:23
 * 功能：请进行修改
 */
public abstract class BaseService<T,ID extends Serializable> {
    private BaseRepository<T,ID> baseRepository;
    public BaseService(BaseRepository<T,ID> baseRepository){
        this.baseRepository=baseRepository;
    }
    protected abstract Search<T> getSearch();

    /**
     * 排序查询所有
     * @param sort
     * @return
     */
    public List<T> findAll(Sort sort){

        return Lists.newArrayList(baseRepository.findAll(sort));
    }

    /**
     * 分页查询
     * @param pageable
     * @return
     */
    public Page<T> findAll(Pageable pageable, SearchCondition... conditions){
        if(conditions==null){
            return baseRepository.findAll(pageable);
        }else {
          //  search=new Search<>(conditions);

            return baseRepository.findAll(getSearch().setSearchConditions(conditions).searchBuild(),pageable);
        }

    }




    /**
     * 保存实体
     * @param entity
     * @return
     */
    public T save(T entity){
        return baseRepository.save(entity);
    }

    /**
     * 批量保存
     * @param entities
     * @return
     */
    public List<T> save(Iterable<T> entities){

        return Lists.newArrayList(baseRepository.save(entities));
    }

    /**
     * 按ID查询
     * @param id
     * @return
     */
    public T findOne(ID id){
        return baseRepository.findOne(id);
    }

    /**
     * 按ID查询是否存在
     * @param id
     * @return
     */
    public boolean exists(ID id){
        return baseRepository.exists(id);
    }

    /**
     * 查找所有
     * @return
     */
    public List<T> findAll(){

        return Lists.newArrayList(baseRepository.findAll());
    }

    /**
     * 查找一组ID
     * @param ids
     * @return
     */
    public List<T> findAll(List<ID> ids){

        return Lists.newArrayList(baseRepository.findAll(ids));
    }

    /**
     * 返回记录数
     * @return
     */
    public long count(){
        return baseRepository.count();
    }

    /**
     * 按ID删除
     * @param id
     */
    public void delete(ID id){
        baseRepository.delete(id);
    }

    /**
     * 按实体删除
     * @param entity
     */
    public void delete(T entity){
        baseRepository.delete(entity);
    }
    public void delete(ID[] ids){
        SearchCondition searchCondition=new SearchCondition();
        searchCondition.setPro("id");
        searchCondition.setOper(SearchOperator.IN);
        searchCondition.setValue(ids);
        List<T> deletEntity=baseRepository.findAll(getSearch().setSearchConditions(searchCondition).searchBuild());
        baseRepository.delete(deletEntity);
    }
    /**
     * 批量删除实体
     * @param entities
     */
    public void delete(Iterable<? extends T> entities){
        baseRepository.delete(entities);
    }

    /**
     * 删除全部
     */
    public void deleteAll(){
        baseRepository.deleteAll();
    }

}
